﻿/*
 * 作者：蒲森
 * 功能描述：套管柱结构
 * 时间：2018-01-18
 * 修改人：
 * 修改时间：
 *
 */
using DataAccess;
using DataBusiness;
using DevExpress.CodeParser;
using DevExpress.XtraEditors;
using Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class frmCasingStru : Form
    {
        private DataTable dispDt;

        public object TextEdit { get; private set; }

        public frmCasingStru()
        {
            InitializeComponent();            
        }

        

        /// <summary>
        /// 加载界面同时加载数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CompletionCasing_Load(object sender, EventArgs e)
        {
            SqlHelper.Open();
            dispDt = SqlHelper.QuerySqlByDataTable("select * from tb_CasingString");
            dispDt.Columns[0].ColumnName = "编号";
            dispDt.Columns[1].ColumnName = "井号";
            dispDt.Columns[2].ColumnName = "套管程序";
            dispDt.Columns[3].ColumnName = "起始位置";
            dispDt.Columns[4].ColumnName = "结束位置";
            dispDt.Columns[5].ColumnName = "规范尺寸(mm)";
            dispDt.Columns[6].ColumnName = "规范扣型";
            dispDt.Columns[7].ColumnName = "长度";
            dispDt.Columns[8].ColumnName = "钢级";
            dispDt.Columns[9].ColumnName = "壁厚(mm)";
            dispDt.Columns[10].ColumnName = "每米重量(kg/m)";
            dispDt.Columns[11].ColumnName = "累重(KN)";
            dispDt.Columns[12].ColumnName = "抗外挤强度(Mpa)";
            dispDt.Columns[13].ColumnName = "抗外挤安全系数";
            dispDt.Columns[14].ColumnName = "抗内压强度";
            dispDt.Columns[15].ColumnName = "抗内压安全系数";
            dispDt.Columns[16].ColumnName = "抗拉强度";
            dispDt.Columns[17].ColumnName = "抗拉安全系数";
            dispDt.Columns[18].ColumnName = "三轴安全系数";
            dispDt.Columns[19].ColumnName = "添加人";
            dispDt.Columns[20].ColumnName = "添加时间";
            gridControl1.DataSource = dispDt;
            DataRow dr = dispDt.NewRow();
            gridView1.BestFitColumns();
        }

        /// <summary>
        /// 删除选中行的所有数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除选中的记录吗？", "删除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2, 0, false) == DialogResult.Yes)
            {
                int selectrowcount = gridView1.SelectedRowsCount;
                if (selectrowcount > 0)
                {
                    string colValue0 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[0]).ToString();
                    tb_CasingStringModel t = new tb_CasingStringModel();
                    CasingStringBLL.DeleteCasingString(colValue0);
                    gridView1.DeleteSelectedRows();
                }
            }
        }

        /// <summary>
        /// 保存对数据的修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Save_Click(object sender, EventArgs e)
        {
            string colValue0 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[0]).ToString();
            string colValue1 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[1]).ToString();
            string colValue2 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[2]).ToString();
            string colValue3 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[3]).ToString();
            string colValue4 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[4]).ToString();
            string colValue5 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[5]).ToString();
            string colValue6 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[6]).ToString();
            string colValue7 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[7]).ToString();
            string colValue8 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[8]).ToString();
            string colValue9 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[9]).ToString();
            string colValue10 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[10]).ToString();
            string colValue11 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[11]).ToString();
            string colValue12 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[12]).ToString();
            string colValue13 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[13]).ToString();
            string colValue14 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[14]).ToString();
            string colValue15 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[15]).ToString();
            string colValue16 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[16]).ToString();
            string colValue17 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[17]).ToString();
            string colValue18 = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.gridView1.Columns[18]).ToString(); 
            tb_CasingStringModel t = new tb_CasingStringModel();
            t.WellNo = colValue1;
            t.CasingProgram = colValue2;
            t.StartLocation = string.IsNullOrEmpty(colValue3) ? 0 : Convert.ToDecimal(colValue3);
            t.EndLocation = string.IsNullOrEmpty(colValue4) ? 0 : Convert.ToDecimal(colValue4);
            t.StandardSize = string.IsNullOrEmpty(colValue5) ? 0 : Convert.ToDecimal(colValue5);
            t.ConnectionType = colValue6;
            t.CasingLength = string.IsNullOrEmpty(colValue7) ? 0 : Convert.ToDecimal(colValue7);
            t.CasingGrade = colValue8;
            t.WallThickness = string.IsNullOrEmpty(colValue9) ? 0 : Convert.ToDecimal(colValue9);
            t.WeightPerMeter = string.IsNullOrEmpty(colValue10) ? 0 : Convert.ToDecimal(colValue10);
            t.CasingWeight = string.IsNullOrEmpty(colValue11) ? 0 : Convert.ToDecimal(colValue11);
            t.ExtrusionStrength = string.IsNullOrEmpty(colValue12) ? 0 : Convert.ToDecimal(colValue12);
            t.ExtrusionSafetyFactor = string.IsNullOrEmpty(colValue13) ? 0 : Convert.ToDecimal(colValue13);
            t.BurstStrength = string.IsNullOrEmpty(colValue14) ? 0 : Convert.ToDecimal(colValue14);
            t.BurstSafetyFactor = string.IsNullOrEmpty(colValue15) ? 0 : Convert.ToDecimal(colValue15);
            t.TensileStrength = string.IsNullOrEmpty(colValue16) ? 0 : Convert.ToDecimal(colValue16);
            t.TensileSafetyFactor = string.IsNullOrEmpty(colValue17) ? 0 : Convert.ToDecimal(colValue17);
            t.TriAxialSafetyFactor = string.IsNullOrEmpty(colValue18) ? 0 : Convert.ToDecimal(colValue18);
            t.UserID = Guid.NewGuid();
            t.InputDate = DateTime.Now;
            //用于判断添加或修改的井号是否在well表里
            List<string> mList = CasingStringBLL.SelectWellNo();
            if (mList.Exists(o => o == colValue1))
            {
                //如果进行操作的这行井号不在在记录内，那么进行增加操作
                try
                {
                    t.ID = Convert.ToInt32(colValue0);
                }
                catch
                {
                    CasingStringBLL.InsertCasingString(t);
                }
                //如果进行操作的这行井号是已在记录内的，那么进行修改操作
                if (colValue0 == Convert.ToString(t.ID))
                {
                    CasingStringBLL.UpdateCasingString(t);
                }
            }
            else
            {
                MessageBox.Show("请输入正确的井号！");
            }
            SqlHelper.Open();
            dispDt = SqlHelper.QuerySqlByDataTable("select * from tb_CasingString");
            dispDt.Columns[0].ColumnName = "编号";
            dispDt.Columns[1].ColumnName = "井号";
            dispDt.Columns[2].ColumnName = "套管程序";
            dispDt.Columns[3].ColumnName = "起始位置";
            dispDt.Columns[4].ColumnName = "结束位置";
            dispDt.Columns[5].ColumnName = "规范尺寸(mm)";
            dispDt.Columns[6].ColumnName = "规范扣型";
            dispDt.Columns[7].ColumnName = "长度";
            dispDt.Columns[8].ColumnName = "钢级";
            dispDt.Columns[9].ColumnName = "壁厚(mm)";
            dispDt.Columns[10].ColumnName = "每米重量(kg/m)";
            dispDt.Columns[11].ColumnName = "累重(KN)";
            dispDt.Columns[12].ColumnName = "抗外挤强度(Mpa)";
            dispDt.Columns[13].ColumnName = "抗外挤安全系数";
            dispDt.Columns[14].ColumnName = "抗内压强度";
            dispDt.Columns[15].ColumnName = "抗内压安全系数";
            dispDt.Columns[16].ColumnName = "抗拉强度";
            dispDt.Columns[17].ColumnName = "抗拉安全系数";
            dispDt.Columns[18].ColumnName = "三轴安全系数";
            dispDt.Columns[19].ColumnName = "添加人";
            dispDt.Columns[20].ColumnName = "添加时间";
            gridControl1.DataSource = dispDt;
            DataRow dr = dispDt.NewRow();
            gridView1.BestFitColumns();
        }
        /// <summary>
        /// 增加空白行用于添加数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Add_Click(object sender, EventArgs e)
        {
            gridView1.AddNewRow(); 
        }
    }
}
